package com.core.image.core;

import com.core.image.utils.CloseUtils;

import java.io.Closeable;
import java.io.IOException;
import java.io.PipedReader;

/**
 * describe:
 *
 * @author 库琦
 * @date 2020/01/24
 */
public abstract class Packet<Stream extends Closeable> implements Closeable {
    /**
     * 最大包大小，5个字节满载组成的Long类型
     */
    public static final long MAX_PACKET_SIZE = (((0xFFL) << 32) | ((0xFFL) << 24) | ((0xFFL) << 16) | ((0xFFL) << 8) | ((0xFFL)));
    public static  final byte TYPE_MEMORY_BYTES = 1;
    public  static final  byte TYPE_MEMORY_STRING = 2;
    public static  final byte TYPE_STREAM_FILE = 3;
    public static  final byte TYPE_STREAM_DIRECT = 4;
    private Stream stream;
    protected long length;
    public abstract Stream createStream();
    public long length(){
        return length;
    }
    protected void closeStream(Stream stream) throws IOException{
        stream.close();
    }
    public byte[] headerInfo(){
        return null;
    }
    public final Stream open(){
        if (stream == null){
            stream = createStream();
        }
        return stream;
    }
    public abstract byte type();

    @Override
    public void close() throws IOException {
        if (stream != null){
            closeStream(stream);
            stream = null;
        }

    }
}
